//
//  PhoneGenJinVc.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/26.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "PhoneGenJinVc.h"

#import "TCAlertView.h"
#import "GenRecordCell.h"
#import "TodoHttpManager.h"
@interface PhoneGenJinVc ()<TCAlertViewDelegate,UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) TCAlertView *alertView;
@property(nonatomic,strong)UITableView *tabView;

//展示录入数据
@property (nonatomic,strong) NSMutableArray *genArr;
@property (nonatomic,strong) NSMutableArray *huiArr;
@property (nonatomic,strong) NSMutableArray *xiaArr;






@end

@implementation PhoneGenJinVc


-(void)viewWillAppear:(BOOL)animated
{
    //来访跟进列表
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuPhoneGenJinBIao:) name:TDGuPhoneGenJinBIao object:nil];
    
    [_manager userPhoneGenJinBiaoWithID:_ID];
    //来访添加记录
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuPhoneGen:) name:TDGuPhoneGen object:nil];
    
    
}

//来访跟进列表
-(void)didTDGuPhoneGenJinBIao:(NSNotification *)notifi
{
    _genArr = [notifi.object objectForKey:@"data"];
    
   NSLog(@"-----%@",_genArr);
    
    [_tabView reloadData];
}

//来访添加跟进
-(void)didTDGuPhoneGen:(NSNotification *)noy
{
    if (noy.object != 0)
    {
        int statue = [[noy.object objectForKey:@"status"] intValue];
        if (statue == 1)
        {
            [_manager MakeMyselfAlert:@"添加成功" andIsNeedTitle:NO andShowTime:2.0];
             [_manager userPhoneGenJinBiaoWithID:_ID];
            [_tabView reloadData];
        }
        else
        {
            [_manager MakeMyselfAlert:@"添加失败" andIsNeedTitle:NO andShowTime:2.0];
        }
    }
    
    [_tabView reloadData];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuPhoneGenJinBIao object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuPhoneGen object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"跟进记录";
    self.view.backgroundColor = BgColor;
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(xinJinBtnClick)];
    
    self.navigationItem.rightBarButtonItem = rightBarItem;
    [self.navigationItem.rightBarButtonItem setImage:[[UIImage imageNamed:@"xinzenggenjin"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    _manager = [TodoHttpManager sharedManager];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 100;
    _tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _tabView.backgroundColor = BgColor;
    
    [self.view addSubview:_tabView];

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _genArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *ID = @"Cell";
    GenRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        cell = [[GenRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    if ([cell isKindOfClass:[GenRecordCell class]])
    {
        if (_genArr.count != 0)
        {
            NSMutableDictionary *dic = [_genArr objectAtIndex:indexPath.row];
            
            [(GenRecordCell *)cell setupGenJinJiLuCellWithGenLab:dic[@"genjin_time"] withHuiKuiLab:dic[@"info"] withXiaLab:dic[@"huifang_time"]];
        }
    }

    
    
    
    return cell;
}
-(void)xinJinBtnClick
{
    [self.alertView showView];//弹出
}

- (void)requestEventAction:(UIButton *)button {
    
    [self.alertView closeView];

    
//    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
//    
//    NSString *uid= [userDefa objectForKey:@"USER_ID"];
//  
  //  [_manager userGuWenPhoneZengID:_ID withGenTime:self.alertView.oneLab.text withHuiMessage:self.alertView.msgField.text withNextTime:self.alertView.xiaLab.text withUserID:uid];

    [self.tabView reloadData];
    
}


-(NSMutableArray *)genArr{
    if (_genArr) {
        return _genArr;
    }
    _genArr = [[NSMutableArray alloc] init];
    return _genArr;
}


-(NSMutableArray *)huiArr{
    if (_huiArr) {
        return _huiArr;
    }
    _huiArr = [[NSMutableArray alloc] init];
    return _huiArr;
}


-(NSMutableArray *)xiaArr{
    if (_xiaArr) {
        return _xiaArr;
    }
    _xiaArr = [[NSMutableArray alloc] init];
    return _xiaArr;
}


- (TCAlertView *)alertView {
    if (!_alertView) {
        self.alertView = [[TCAlertView alloc] initWithFrame:CGRectMake(30, 200, [UIScreen mainScreen].bounds.size.width - 60, 220)];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.delegate = self;
    }
    return _alertView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
